package wikiPhilia_two;

import processing.core.PApplet;
import processing.core.PImage;
import processing.core.PShape;
import processing.core.PVector;

public class WNode {
	public static final int TYPE_ARTICLE = 0;
	public static final int TYPE_PERSON = 1;
	public static final int TYPE_NON_PERSON = 2;
	public static final int TYPE_CATEGORY = 3;
	public static final int TYPE_UNDEF = 4;
	public static final int TYPE_PERSON_ZERO = 5;
	public static final int STATE_DEFAULT = 0;
	public static final int STATE_HIGHLIGHTED = 1;
//	public static final int STATE_SELECTED = 2;
	public static final int LEVEL_ZERO = 0;
	public static final int LEVEL_ONE = 1;
	public static final int LEVEL_TWO = 2;
	public static final int LOOP= 6;
	private PApplet parent;
	private PVector pos;
	private PVector cPos; //= current position
	private PVector tPos; //= target position
	public static final float ACC = 0.05f;
	private String title;
	private int type;
	private int level;
	private int state = STATE_DEFAULT;
	private WArticle wItem;
	private int[] linksCount, backlinksCount, superCategoriesCount, subCategoriesCount;
	private int subItemsCount, superCatsCount;
	
	//private PImage myPlanet; 
	//private PImage network; 
	//private PShape svg; 
	float distance = 200; 
	float catAlpha = 50; 

	
	public WNode(PApplet parent, String title) {
		this.parent = parent;
		this.title = title;
		linksCount = new int[4]; //for level 0 through 2, 3 = total
		backlinksCount = new int[4];
		pos = new PVector();
		//pos = new PVector(parent.random(-2000,2000),parent.random(-2000,2000),parent.random(-2000,2000));
		tPos = pos;
		if (cPos == null) cPos = tPos;
	}
	public WNode(PApplet parent, String title, int type) {
		this.parent = parent;
		this.title = title; 
	    this.type = type;
		linksCount = new int[4]; //for level 0 through 2, 3 = total
		backlinksCount = new int[4];
		pos = new PVector();
		//pos = new PVector(parent.random(-2000,2000),parent.random(-2000,2000),parent.random(-2000,2000));
		tPos = pos;
		if (cPos == null) cPos = tPos;
	}
	
	public WNode(PApplet parent, String title, int type, int level) {
		this.parent = parent;
		this.title = title; 
	    this.type = type;
	    this.level = level;
		linksCount = new int[4]; //for level 0 through 2, 3 = total
		backlinksCount = new int[4];
		pos = new PVector();
		//pos = new PVector(parent.random(-2000,2000),parent.random(-2000,2000),parent.random(-2000,2000));
		tPos = pos;
		if (cPos == null) cPos = tPos;
	}

//	public WNode(PApplet parent, String title, int type,
//			PImage planet) {
//		this.parent = parent;
//		this.title = title; 
//	    this.type = type;
////		linksCount = 0;
////		backlinksCount = 0;
//		pos = new PVector();
//		//pos = new PVector(parent.random(-2000,2000),parent.random(-2000,2000),parent.random(-2000,2000));
//		tPos = pos;
//		if (cPos == null) cPos = tPos;
//		this.myPlanet = planet; 
//		
//	}
	
//	public WNode(PApplet parent, String title, int type,
//			PImage planet, PImage network) {
//		this.parent = parent;
//		this.title = title; 
//	    this.type = type;
////		linksCount = 0;
////		backlinksCount = 0;
//		pos = new PVector();
//		//pos = new PVector(parent.random(-2000,2000),parent.random(-2000,2000),parent.random(-2000,2000));
//		tPos = pos;
//		if (cPos == null) cPos = tPos;
//		this.myPlanet = planet; 
//		//this.svg = svg; 
//		this.network = network; 
//	}
	
	public WNode(PApplet parent, WArticle wItem) {
		this.parent = parent;
		this.wItem = wItem;
		this.title = wItem.getTitle();
	}
	
	public void display() {
		parent.pushStyle();
		//parent.strokeWeight(5*linksCount/(float)WikiPhilia_002.maxLinkCount+1f);			
		switch (type) {
		case TYPE_ARTICLE:
			parent.stroke(255,255,0,127);	
			switch (state) {
			case STATE_DEFAULT:
				break;
			case STATE_HIGHLIGHTED:
//				parent.pushStyle();
//				parent.pushMatrix();
//				parent.noStroke();
//				parent.fill(255);
//				parent.translate(cPos.x, cPos.y,cPos.z);
//				parent.rotateY(-WikiPhilia_two_004.yAngle);
//				parent.rotateX(-WikiPhilia_two_004.xAngle);
//				
//				parent.text(title,0,0,0);
//				parent.popMatrix();
//				parent.popStyle();
//				parent.strokeWeight(6f);
					//	shape(s);
				
				parent.pushMatrix();
				parent.translate(cPos.x, cPos.y,cPos.z);
				parent.rotateY(-WikiPhilia_two_006d.yAngle);
				parent.rotateX(-WikiPhilia_two_006d.xAngle);
				
				parent.pushStyle();
				parent.tint(200,255,255, 250); 
				//parent.image(WikiPhilia_two_006d.planet, -WikiPhilia_two_006d.planet.width/2, -WikiPhilia_two_006d.planet.width/2, 50, 50);
				parent.image(WikiPhilia_two_006d.planet, -25, -25, 50, 50);
			
					//title
				parent.pushStyle();
				parent.fill(255, distance);
				parent.textSize(6);
					parent.text(title,0,-5,0);
					float textwidth = parent.textWidth(title); 
				parent.popStyle(); 	
					
				parent.pushMatrix();
			//	parent.translate(-5,-8,0);
				parent.translate(textwidth-20,-10,0); 

				parent.popStyle();
				parent.pushStyle(); 
					float width = 25; 
					float height = 25;
					//float myLens = parent.map(cPos.z, -200, 200, 0, 255); 
					//System.out.println(cPos.z);
					parent.tint(255,200);
					//parent.translate(width/2, -height+5);
					int w = WikiPhilia_two_006d.network.width;
					int h = WikiPhilia_two_006d.network.height;
					parent.translate(width/2, height-5);
					parent.image(WikiPhilia_two_006d.network,-width/2, -height+5, width, height);
					//parent.image(WikiPhilia_two_006d.network,0,0, width, height);
					//parent.fill(255,0,0);
					//parent.rect (0, 0, 20, 20); 
					//parent.image(WikiPhilia_two_006d.network,0, 0, width, height);
				parent.popStyle();
				//parent.tint(255, 0,0);
				String links = String.valueOf(linksCount[3]);//.toString(); 
				parent.translate(-25, 0);
				//main-counter
				parent.textSize(3);
				parent.fill(19,114,123); 
				parent.textAlign(parent.CENTER);
				parent.text(String.valueOf(backlinksCount[3]-1), 20, -3.5f, 0); //backlinks-network; 
			
				parent.textSize(4); 
				parent.fill(255, 90); 
				parent.textAlign(parent.RIGHT); 
				parent.text(String.valueOf(linksCount[3]), 16, -9, 0);
				//if (linksCount[3] < linksCount[2]) System.out.println(" total: " + linksCount[3] + " l2: " + linksCount[2]);
				
				//p1
				parent.textSize(3); 
				parent.textAlign(parent.LEFT); 
				parent.fill(19,114,123); 
				parent.text(String.valueOf(backlinksCount[2]), 37, - 12, 0); 
				
				//p2
				parent.textAlign(parent.LEFT); 
				parent.fill(19,114,123); 
				//parent.fill(19,0,123); 
				parent.text(String.valueOf(backlinksCount[1]), 24, - 18, 0); 
				parent.popMatrix(); 
				
			
				
				parent.popMatrix();
				
				parent.strokeWeight(6f);

				break;
//			case STATE_SELECTED:
//				parent.stroke(255,255,0);	
//				parent.strokeWeight(9f);
//				break;

			default:
				break;
			}
			break;
		case TYPE_CATEGORY:
			parent.strokeWeight(5f);			
			parent.stroke(0,255,127,127);	
			break;
//		case TYPE_PERSON_ZERO:
//			//parent.strokeWeight(20f);			
//			parent.stroke(255,0,0);	
//			parent.pushStyle();
//			parent.pushMatrix();
//			parent.noStroke();
//			parent.fill(255);
//			parent.translate(cPos.x, cPos.y,cPos.z);
//		//	parent.rotateY(-WikiPhilia_006.yAngle);
//		//	parent.rotateX(-WikiPhilia_006.xAngle);
//			parent.text(title,0,0,0);
//			parent.popMatrix();
//			parent.popStyle();
//
//			break;
		case LOOP:
			
			parent.pushMatrix();
			parent.pushStyle();
				parent.strokeWeight(2f);
				parent.stroke(255,0,0,127);
				parent.fill(255); 
				parent.translate(cPos.x, cPos.y,cPos.z);
				parent.rotateY(-WikiPhilia_two_006d.yAngle);
				parent.rotateX(-WikiPhilia_two_006d.xAngle);
				parent.text(title, 0, 0, 0);
				//parent.text ("irgendwas", 0, 0, 0); 
				//	parent.box(cPos.x, cPos.y, cPos.z, 150);
			parent.popMatrix();
			parent.popStyle(); 
			
			break;
		
			
		case TYPE_PERSON_ZERO:
			//parent.strokeWeight(20f);	
			parent.pushMatrix();
				parent.translate(cPos.x, cPos.y,cPos.z);
				parent.rotateY(-WikiPhilia_two_006d.yAngle);
				parent.rotateX(-WikiPhilia_two_006d.xAngle);
			
			parent.pushStyle();
				parent.fill(19,114,123); 
				parent.text(title,0,0,0);
				parent.popMatrix();
			parent.popStyle();


			break;	
	

		default:
			break;
		}
		parent.popStyle(); 
		parent.pushStyle(); 
		parent.beginShape(parent.POINT);
		parent.strokeWeight(2f);
		parent.noFill(); 
		//parent.tint(255, 20); 
		parent.stroke(255,50); 
		//parent.image(planet, 0,0);
		parent.vertex(cPos.x,cPos.y,cPos.z);
		parent.endShape();
		parent.popStyle();
	
	

		
		
		
//		parent.beginShape(parent.POINT);
//		parent.vertex(cPos.x,cPos.y,cPos.z);
//		parent.endShape();
//		parent.popStyle();
	}
	
	public void update () {
		cPos.x = (tPos.x-cPos.x)*ACC + cPos.x;
		cPos.y = (tPos.y-cPos.y)*ACC + cPos.y;
		cPos.z = (tPos.z-cPos.z)*ACC + cPos.z;
	}
	
	public void setPos(PVector pos) {
		this.pos = pos;
		tPos = pos;
	}
	public PVector getPos() {
		return cPos;
	}
	public void setParent(PApplet parent) {
		this.parent = parent;
	}
	public PApplet getParent() {
		return parent;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getTitle() {
		return title;
	}
	public void setType(int type) {
		this.type = type;
	}
	public int getType() {
		return type;
	}
	public void setwItem(WArticle wItem) {
		this.wItem = wItem;
	}
	public WArticle getwItem() {
		return wItem;
	}
	public int getTotalLinksCount() {
		return linksCount[3];
	}
	
	public int getTotalBacklinksCount() {
		return backlinksCount[3];
	}
	
	public void setBacklinksCount(int index, int backlinksCount) {
		this.backlinksCount[index] = backlinksCount;
	}
	public int getBacklinksCount(int index) {
		return backlinksCount[index];
	}
	public void setLinksCount(int index, int linksCount) {
		this.linksCount[index] = linksCount;
	}
	public void incLinks(int index) {
		linksCount[index]++;
		linksCount[3]++;
	}
	public void incBacklinks(int index) {
		backlinksCount[index]++;
		backlinksCount[3]++;
	}
	public int getLinksCount(int index) {
		return linksCount[index];
	}
	public void setSubCategoriesCount(int index, int subCategoriesCount) {
		this.subCategoriesCount[index] = subCategoriesCount;
	}
	public int getSubCategoriesCount(int index) {
		return subCategoriesCount[index];
	}
	public void setSuperCategoriesCount(int index, int superCategoriesCount) {
		this.superCategoriesCount[index] = superCategoriesCount;
	}
	public int getSuperCategoriesCount(int index) {
		return superCategoriesCount[index];
	}
	@Override
	public String toString() {
		return " " + title + ", linksCount= " + linksCount + ", backlinksCount= " + backlinksCount + "]" + "\n";
	}
	public PVector getcPos() {
		return cPos;
	}
	public void setcPos(PVector cPos) {
		this.cPos = cPos;
	}
	public PVector gettPos() {
		return tPos;
	}
	public void settPos(PVector tPos) {
		this.tPos = tPos;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	public void setLevel(int level) {
		this.level = level;
	}
	public int getLevel() {
		return level;
	}
	public void setSuperCatsCount(int superCatsCount) {
		this.superCatsCount = superCatsCount;
	}
	public int getSuperCatsCount() {
		return superCatsCount;
	}
	public void setSubItemsCount(int subItemsCount) {
		this.subItemsCount = subItemsCount;
	}
	public int getSubItemsCount() {
		return subItemsCount;
	}
	
}
